package factorytest.iflytek.com.einkiflytekfactorytest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.android.server.roobo.NVRomIntegerInfo;
import com.android.server.roobo.NVRomStringInfo;
import com.android.server.roobo.RobotNVRomDataLocalJni;

import java.io.File;

import factorytest.iflytek.com.einkiflytekfactorytest.agingtest.AgingTestItemAttr;
import factorytest.iflytek.com.einkiflytekfactorytest.config.OTATestConfig;
import factorytest.iflytek.com.einkiflytekfactorytest.config.PCCommand;
import factorytest.iflytek.com.einkiflytekfactorytest.engineerui.EngineerTestItemsEnum;
import factorytest.iflytek.com.einkiflytekfactorytest.factorytestui.FactoryTestItemsEnum;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DebugLog;
import factorytest.iflytek.com.einkiflytekfactorytest.common.FTUtils;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.FileReadWrite;

/**
 * Created by sst on 2018/7/17.
 */

public class PCCommandReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (TextUtils.isEmpty(action)) {
            return;
        }

        DebugLog.e( "action = " + action);

        String pass = context.getString(R.string.test_result_pass);
        String fail = context.getString(R.string.test_result_fail);
        boolean startMain = true;

        if (action.equals(PCCommand.PC_ACTION_SET_AGING_FAILED)) {

            if (AgingTestItemAttr.isAgingTestEnabled(context)) {
                startMain = false;
            }

            FTUtils.clearAgingResult(context);


        } else if (action.equals(PCCommand.PC_ACTION_SET_AGING_ITEM_FORCE_TEST_OVER)) {

            int id = intent.getIntExtra(PCCommand.PC_ACTION_AGING_ITEM_INT_EXTRA, -1);

            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                if (itemAttr.getId() == id) {
                    itemAttr.setForceTestOver(context, true);
                    break;
                }
            }

            startMain = false;

        } else if (action.equals(PCCommand.PC_ACTION_CLR_AGING_ITEM_FORCE_TEST_OVER)) {

            int id = intent.getIntExtra(PCCommand.PC_ACTION_AGING_ITEM_INT_EXTRA, -1);

            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                if (itemAttr.getId() == id) {
                    itemAttr.setForceTestOver(context, false);
                    break;
                }
            }

            startMain = false;

        } else if (action.equals(PCCommand.PC_ACTION_SET_AGING_ITEM_TEST_OVER_RATIO)) {

            int id = intent.getIntExtra(PCCommand.PC_ACTION_AGING_ITEM_INT_EXTRA, -1);
            float ratio = intent.getFloatExtra(PCCommand.PC_ACTION_AGING_ITEM_RATIO_FLOAT_EXTRA, 1.0f);

            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                if (itemAttr.getId() == id) {
                    if (ratio > 0) {
                        itemAttr.setRatioForTestOver(context, ratio);
                    }
                    break;
                }
            }

            startMain = false;

        } else if (action.equals(PCCommand.PC_ACTION_SET_ALL_PASSED)) {

            for (EngineerTestItemsEnum item : EngineerTestItemsEnum.values()) {
                item.setResult(context, pass);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, pass);
                }
            }

            for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {
                item.setResult(context, pass);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, pass);
                }
            }

        } else if (action.equals(PCCommand.PC_ACTION_SET_ALL_FAILED)) {

            for (EngineerTestItemsEnum item : EngineerTestItemsEnum.values()) {
                item.setResult(context, fail);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, fail);
                }
            }

            for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {
                item.setResult(context, fail);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, fail);
                }
            }

            for (AgingTestItemAttr itemAttr : AgingTestItemAttr.values()) {
                itemAttr.clearAll(context);
            }

            AgingTestItemAttr.clearAgingAll(context);

        } else if (action.equals(PCCommand.PC_ACTION_SET_ITEM_PASSED)) {

            int id = intent.getIntExtra(PCCommand.PC_ACTION_SET_ITEM_PASSED_INT_EXTRA, -1);
            EngineerTestItemsEnum item = EngineerTestItemsEnum.getItemEnumByIdentify(id);
            if (item != null) {
                item.setResult(context, pass);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, pass);
                }
            }

            FactoryTestItemsEnum factoryTestItems = FactoryTestItemsEnum.getItemEnumByIdentify(id);
            if (factoryTestItems != null) {
                factoryTestItems.setResult(context, pass);
                if (factoryTestItems.isKeepTestAfterAging()) {
                    factoryTestItems.setResultForAfterAging(context, pass);
                }
            }


        } else if (action.equals(PCCommand.PC_ACTION_SET_ITEM_FAILED)) {

            int id = intent.getIntExtra(PCCommand.PC_ACTION_SET_ITEM_FAILED_INT_EXTRA, -1);
            EngineerTestItemsEnum item = EngineerTestItemsEnum.getItemEnumByIdentify(id);
            if (item != null) {
                item.setResult(context, fail);
                if (item.isKeepTestAfterAging()) {
                    item.setResultForAfterAging(context, fail);
                }
            }

            FactoryTestItemsEnum factoryTestItems = FactoryTestItemsEnum.getItemEnumByIdentify(id);
            if (factoryTestItems != null) {
                factoryTestItems.setResult(context, fail);
                if (factoryTestItems.isKeepTestAfterAging()) {
                    factoryTestItems.setResultForAfterAging(context, fail);
                }
            }

        } else if (action.equals(PCCommand.PC_ACTION_SET_AGING_DISABLED)) {

            AgingTestItemAttr.setAgingTestEnabled(context, false);

            OTATestConfig.setEssentialForOTATest(context, false);

            startMain = false;

        } else if (action.equals(PCCommand.PC_ACTION_SHOW_NVROM_DATA)) {

            startMain = false;

            String filePath = FTUtils.getFTStorageDirectory() + File.separator + "nvrom.log";
            File file = new File(filePath);
            if (file.exists()) {
                file.deleteOnExit();
            }
            RobotNVRomDataLocalJni nvrom = RobotNVRomDataLocalJni.getInstance();
            if (nvrom.getCountOfAllStringNVRom() > 0) {
                NVRomStringInfo[] stringInfos = nvrom.getAllStringNVRom(0, nvrom.getCountOfAllStringNVRom());
                if (stringInfos != null) {
                    for (NVRomStringInfo info : stringInfos) {
                        FileReadWrite.writeAppendFile(filePath, info.toString());
                    }
                }
            }


            if (nvrom.getCountOfAllIntegerNVRom() > 0) {
                NVRomIntegerInfo[] integerInfos = nvrom.getAllIntegerNVRom(0, nvrom.getCountOfAllIntegerNVRom());
                if (integerInfos != null) {
                    for (NVRomIntegerInfo info : integerInfos) {
                        FileReadWrite.writeAppendFile(filePath, info.toString());
                    }
                }
            }

        } else if (action.equals(PCCommand.PC_ACTION_CLEAR_EMMC_TEST_FLAG)) {

            AgingTestItemAttr.setHardwareEmmcTestFailed(context, false);
        }

        if (startMain) {
            PCCommand.setFromPCBroadcast(context);
            FTUtils.startMainActivity(context);
        }

    }
}
